package com.demo.project_template.util;


import java.util.Collection;
import java.util.Map;

/**
 * 判空类
 * @author ZhangKeChen
 * @date 2022/8/30
 */

public class CheckEmptyUtil {


    /**
     * 判断对象是否为空
     *
     * @param value
     * @return
     */
    public static <T> boolean isEmpty(T value) {
        if (value == null) {
            return true;
        } else if (value instanceof String) {
            return ((String) value).length() == 0;
        } else if (value instanceof Collection) {
            return ((Collection) value).isEmpty();
        } else if (value instanceof Map) {
            return ((Map) value).size() == 0;
        } else if (value instanceof String[]) {
            return ((String[]) ((String[]) value)).length == 0;
        } else {
            return false;
        }
    }

    /**
     * 判断字符串是否为空
     *
     * @param s
     * @return
     */
    public static boolean isEmpty(String s) {
        return s == null || s.length() == 0;
    }

    /**
     * 判断集合是否为空
     *
     * @param c
     * @return
     */
    public static boolean isEmpty(Collection c) {
        return c == null || c.isEmpty();
    }

    /**
     * 判断字符串是否不为空
     *
     * @param s
     * @return
     */
    public static boolean isNotEmpty(String s) {
        return s != null && s.length() > 0;
    }

    /**
     * 判断集合是否不为空
     *
     * @param c
     * @return
     */
    public static boolean isNotEmpty(Collection c) {
        return c != null && !c.isEmpty();
    }

    /**
     * 判断对象是否为字符串
     *
     * @param obj
     * @return
     */
    public static <T> boolean isString(T obj) {
        return obj instanceof String;
    }
}
